package peterka.ecka;

import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import peterka.ecka.model.EItem;

public class Midlet extends MIDlet implements CommandListener {

    Display display;
    Command commandExit;
    Command commandAbout;
    Command commandSearch;
    Command commandSubmitSearch;
    Canvas canvas;
    Form formAbout;
    TextField searchField;
    Vector data;

    public Midlet() {
        commandExit = new Command("Konec", Command.EXIT, 1);
        commandAbout = new Command("Info", Command.SCREEN, 3);
        commandSearch = new Command("Vyhledat", Command.SCREEN, 2);
        commandSubmitSearch = new Command("Hledat", Command.SCREEN, 1);
    }

    public void startApp() {
        loadData();

        display = getDisplay();
        canvas = new Splash();
        canvas.addCommand(commandAbout);
        canvas.addCommand(commandExit);
        canvas.addCommand(commandSearch);
        canvas.setCommandListener(this);
        display.setCurrent(canvas);
    }

    protected void loadData() {
        DataReader reader = new DataReader();
        data = reader.read();
    }

    public void pauseApp() {
    }

    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    public void commandAction(Command c, Displayable d) {
        // zde vyhodnotíme výběr položky z nabídky a podle toho zavoláme příslušnou metodu
        //if (d == canvas) { // pokud zjistíme, zda je příkaz volaný z úvodní obrazovky
        if (c == commandExit) {
            destroyApp(true);
        } // a pokud byl vybrán příkaz konec ukončíme aplikaci
        if (c == commandAbout) {
            info();
        } // na příkaz about ukážeme informace o aplikaci
        if (c == commandSearch) {
            search("E");
        }
        if (c == commandSubmitSearch) {
            searchSubmit();
        }
        //}
    }

    public void info() {
        // zobrazíme info
        display = getDisplay();
        formAbout.addCommand(commandExit);
        formAbout.setCommandListener(this);
        String about = "Hledator ecek - dobrot v potravinach.\n"
                + "Autor: Martin Peterka.\n"
                + "Inspirovano aplikaci Ecka od \"Filip Maly\".";
        formAbout = new Form("Info");
        formAbout.append(about);
        display.setCurrent(formAbout);
    }

    public Display getDisplay() {
        return Display.getDisplay(this);
    }

    public void search(String defaultInput) {
        display = getDisplay();

        Form searchForm = new Form("Vyhledat");
        searchField = new TextField("Nazev", defaultInput, 10, TextField.NON_PREDICTIVE);
        searchForm.append(searchField);
        searchForm.addCommand(commandSubmitSearch);
        searchForm.setCommandListener(this);
        display.setCurrent(searchForm);
    }

    public void searchSubmit() {
        String searchTerm = searchField.getString();
        Logger.debug("Searching: " + searchTerm);
        StringBuffer sb = new StringBuffer();
        Vector found = Finder.find(data, searchTerm);

        if (found.size() > 10) {
            sb.append("Moc zaznamu, upresnete hledani.");
        } else {
            for (int i = 0; i < found.size(); i++) {
                EItem eItem = (EItem) found.elementAt(i);
                Logger.debug(eItem);
                sb.append(eItem.format());
                sb.append("--------");
            }
        }

        Form resultForm = new Form("Vysledky");
        resultForm.append(sb.toString());
        resultForm.addCommand(commandSearch);
        resultForm.setCommandListener(this);
        display.setCurrent(resultForm);
    }
}
